﻿//========================================================= 
/**@file MemoryCard_TestCase.h
 * @brief 存储卡IMemoryCard接口测试案例
 * 
 * @date 2016-08-13   10:17:30
 * @author Zhyioo 
 * @version 1.0
 */ 
//--------------------------------------------------------- 
#ifndef _LIBZHOUYB_MEMORYCARD_TESTCASE_H_
#define _LIBZHOUYB_MEMORYCARD_TESTCASE_H_
//--------------------------------------------------------- 
#include "../TestFrame.h"

#include "../../../device/iccard/extension/base/IMemoryCard.h"
using zhou_yb::device::iccard::extension::base::IMemoryCard;
//--------------------------------------------------------- 
namespace zhou_yb {
namespace application {
namespace test {
//--------------------------------------------------------- 
/// 存储卡测试案例
struct MemoryCard_TestCase : public ITestCase<IMemoryCard>
{
    /// 校验用户密码读写测试区 
    virtual bool Test(Ref<IMemoryCard>& testObj, const ByteArray& testArg, TextPrinter& printer)
    {
        printer.TextPrint(TextPrinter::TextNote, "存储卡测试");

        ByteBuilder key(8);
        size_t adr = 0;
        size_t len = 8;

        StringArguments arg;
        ArgParser parser;
        if(!parser.Parse(testArg, arg))
        {
            DevCommand::FromAscii(testArg, key);
        }
        else
        {
            arg["Address"].Get<size_t>(adr);
            arg["Length"].Get<size_t>(len);
            arg["Key"].Get<ByteBuilder>(key);
        }

        if(!testObj->VerifyKey(key))
        {
            printer.TextPrint(TextPrinter::TextLogger, "校验用户密码失败");
            return false;
        }
        
        ByteBuilder random(8);
        ByteConvert::Random(random, len);
        ByteBuilder tmp(8);
        ByteConvert::ToAscii(random, tmp);

        printer.TextPrint(TextPrinter::TextLogger, tmp.GetString());
        tmp.Clear();

        if(!testObj->WriteMemory(adr, random))
        {
            printer.TextPrint(TextPrinter::TextLogger, "写数据失败");
            return false;
        }

        if(!testObj->ReadMemory(adr, len, tmp))
        {
            printer.TextPrint(TextPrinter::TextLogger, "写入后读数据失败");
            return false;
        }

        if(!random.IsEqual(tmp))
        {
            printer.TextPrint(TextPrinter::TextLogger, "数据比对失败");
            return false;
        }
        return true;
    }
};
//--------------------------------------------------------- 
} // namespace test 
} // namespace application 
} // namespace zhou_yb 
//--------------------------------------------------------- 
#endif // _LIBZHOUYB_MEMORYCARD_TESTCASE_H_
//========================================================= 